package com.exam.Controller;

import com.exam.Entity.ApiResult;
import com.exam.Entity.Teacher;
import com.exam.Service.TeacherService;
import com.exam.util.ApiResultHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;
import java.util.Map;

@RestController
public class TeacherController {

    private TeacherService teacherService;
    @Autowired
    public TeacherController(TeacherService teacherService){
        this.teacherService = teacherService;
    }

    @GetMapping("/teachers/{page}/{size}")
    public ApiResult findAll(@PathVariable Integer page, @PathVariable Integer size){
//        Page<Teacher> teacherPage = new Page<>(page,size);
//        IPage<Teacher> teacherIPage = teacherService.findAll(teacherPage);
//
//        return ApiResultHandler.buildApiResult(200,"查询所有教师",teacherIPage);
        ApiResult apiResult;
        List<Teacher> res = teacherService.findAll(page,size);
        List<Teacher> t = teacherService.findTotal(page,size);
        // 构造返回数据
        Map<String, Object> data = new java.util.HashMap<>();
        data.put("records", res);               // 返回的试卷列表
        data.put("current", page);              // 当前页码
        data.put("size", size);                 // 每页大小
        data.put("total", t.size());          // 总记录数（列表长度）

        // 构建 ApiResult
        apiResult = ApiResultHandler.buildApiResult(200, "请求成功！", data);

        return apiResult;
    }

    @GetMapping("/teacher/{teacherId}")
    public ApiResult findById(@PathVariable("teacherId") Integer teacherId){
        return ApiResultHandler.success(teacherService.findById(teacherId));
    }

    @DeleteMapping("/teacher/{teacherId}")
    public ApiResult deleteById(@PathVariable("teacherId") Integer teacherId){
        return ApiResultHandler.success(teacherService.deleteById(teacherId));
    }

    @PutMapping("/teacher")
    public ApiResult update(@RequestBody Teacher teacher){
        return ApiResultHandler.success(teacherService.update(teacher));
    }

    @PostMapping("/teacher")
    public ApiResult add(@RequestBody Teacher teacher){
        return ApiResultHandler.success(teacherService.add(teacher));
    }
}
